package com.fwzc.giuseppe.dao;

import java.util.ArrayList;
import com.fwzc.giuseppe.db.DBHelper;
import com.fwzc.giuseppe.entity.UserKey;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserKeyDao {
	private DBHelper helper;
	public UserKeyDao(Context context){
		helper = new DBHelper(context);
	}
	public void addUserKey(UserKey userKey){
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("userId", userKey.userId);
			values.put("userIdentityNumber", userKey.userIdentityNumber);
			values.put("name", userKey.name);
			values.put("headPortrait", userKey.headPortrait);
			db.insert("userkey", null, values);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.close();
		}
	}
	
	public ArrayList<UserKey> findUserKey(){
		ArrayList<UserKey> dataList = new ArrayList<UserKey>();
		SQLiteDatabase db = helper.getReadableDatabase();
		try {
			Cursor c = db.rawQuery("select * from userkey", null);
			while(c.moveToNext()){
				UserKey userkey = new UserKey();
				userkey.userId = c.getString(c.getColumnIndex("userId"));
				userkey.userIdentityNumber = c.getString(c.getColumnIndex("userIdentityNumber"));
				userkey.name = c.getString(c.getColumnIndex("name"));
				userkey.headPortrait = c.getString(c.getColumnIndex("headPortrait"));
				dataList.add(userkey);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.close();
		}
		return dataList;
	}
	

	public void deleteInfo(){
		SQLiteDatabase db = helper.getWritableDatabase();
		try {
			db.delete("userkey", null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.close();
		}
	}
	
	public void updateInfo(String userId,UserKey userKey){
		SQLiteDatabase db = helper.getReadableDatabase();
		try {
			ContentValues values = new ContentValues();
			values.put("headPortrait", userKey.headPortrait);
			values.put("name", userKey.name);
			db.update("userkey", values, "userId = ?", new String[]{userId});
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			db.close();
		}
	}
}
